//
//  NormalTableView.m
//  StationManager
//
//  Created by 张佳强 on 17/3/8.
//  Copyright © 2017年 Helong. All rights reserved.
//

#import "NormalTableView.h"
#import "DataCell.h"
#import "PhotoCell.h"
@implementation NormalTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    
    self = [super initWithFrame:frame style:style];
    if (self) {
        
        self.delegate = self;
        self.dataSource = self;
        self.showsVerticalScrollIndicator = NO;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.dataArray = [NSMutableArray arrayWithCapacity:0];
        NSDictionary * dict = [self jsonDict];
        NSArray * origin = dict[@"origin"];
        [self.dataArray addObjectsFromArray:origin];
        self.cellIndex = 999;
        
    }
    return self;
}

-(void)setImageData{
    UIImage * originImage = [UIImage imageNamed:@"add_pics"];
//    NSArray * photoOriginArray = @[originImage,originImage,originImage,originImage,originImage,originImage,originImage,originImage,originImage];
    
    if ([self.stationModel.stationType isEqualToString:@"T1"]) {
        
        _titleArray = @[@"全景",@"东",@"西",@"南",@"北",@"太阳能板",@"端子连接线",@"雨量筒翻斗",@"雨量筒上侧"];
    }
    else if ([self.stationModel.stationType isEqualToString:@"T2"] || [self.stationModel.stationType isEqualToString:@"G2"]){
        
        _titleArray = @[@"全景",@"东",@"西",@"南",@"北",@"太阳能板",@"端子连接线",@"雨量筒翻斗",@"雨量筒上侧",@"温度罩"];

    }
    else if ([self.stationModel.stationType isEqualToString:@"T4"] || [self.stationModel.stationType isEqualToString:@"T6"]){
        
        _titleArray = @[@"全景",@"东",@"西",@"南",@"北",@"太阳能板",@"端子连接线",@"雨量筒翻斗",@"雨量筒上侧",@"百叶箱",@"避雷针",@"拉线"];
        
    }
    self.photoTagArray = [NSMutableArray arrayWithCapacity:0];
    self.photoArray = [NSMutableArray arrayWithCapacity:0];

    NSMutableArray *photoOriginTagArray = [NSMutableArray arrayWithCapacity:0];
    NSMutableArray *photoOriginArray = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i<_titleArray.count; i++) {
        
        [photoOriginTagArray addObject:@-1];
        [photoOriginArray addObject:originImage];
    }
    
//    NSArray * photoOriginTagArray = @[@-1,@-1,@-1,@-1,@-1,@-1,@-1,@-1,@-1];
    [self.photoTagArray addObjectsFromArray:photoOriginTagArray];
    [self.photoArray addObjectsFromArray:photoOriginArray];
    
    
}

//解析本地json数据
- (NSDictionary *)jsonDict {
    NSString * filePath = [[NSBundle mainBundle] pathForResource:@"Maintenance" ofType:@"json"];
    NSData * jsonData = [[NSData alloc]initWithContentsOfFile:filePath];
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
    NSDictionary * dict = jsonObject[@"normal"];
    return dict;
}

//移除菜单
- (void)hide {
    
    [self.listTableView removeFromSuperview];
    self.listTableView = nil;
    [self.bgView removeFromSuperview];
    self.bgView = nil;
}

#pragma Notifivation - delegete

//更改常用信息的数据源
- (void)changeDataSource:(NSNotification *)notify {
    
    NSDictionary * dict = (NSDictionary *)notify.object;
    NSInteger row = [dict[@"row"] integerValue];
    NSInteger section = [dict[@"section"] integerValue];
    NSMutableArray * array = [[NSMutableArray alloc]initWithCapacity:0];
    [array addObjectsFromArray:self.dataArray[section]];
    [array replaceObjectAtIndex:row withObject:dict[@"data"]];
    [self.dataArray replaceObjectAtIndex:section withObject:array];
    [self reloadData];
    [self.listTableView removeFromSuperview];
    self.listTableView = nil;
    [self.bgView removeFromSuperview];
    self.bgView = nil;
}

#pragma TableView - delegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeDataSource:) name:@"changeData" object:nil];
    UILabel * headText = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
    headText.font = [UIFont systemFontOfSize:15.0];
    NSDictionary * dict = [self jsonDict];
    NSArray * textArray = dict[@"headText"];
    if (section == 0) {
        self.headView =[[HeadView alloc]init];
        self.headView.stationName.text = self.stationModel.stationName;
        self.headView.stationNumber.text =self.stationModel.stationNum;
        self.headView.dateTimeLabel.text = _currentTime;
        self.headView.frame =CGRectMake(0, 0, SCREEN_WIDTH, 225);
        self.headView.personTextField.delegate = self;
        if (self.personData == nil) {
            self.headView.personTextField.text = @"";
        }else{
            self.headView.personTextField.text = self.personData;
        }
        return self.headView;
    }
    else{
        headText.text = textArray[section-1];
        return headText;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0 ) {
        return 225;
    }else{
        return 35;
    }
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 4;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section<2) {
        return 3;
    }else{
        return  1;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if (indexPath.section<2) {
            static NSString * iden = @"data";
            DataCell * cell = [tableView cellForRowAtIndexPath:indexPath];
            if (cell == nil) {
                cell = [[DataCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
            }
            cell.dataLabel.text = self.dataArray[indexPath.section][indexPath.row];
            if (indexPath.row%2 == 0) {
                cell.dataLabel.frame = CGRectMake(90, 2, SCREEN_WIDTH-90, 36);
                cell.spaceLabel.frame = CGRectMake(80, 2, 10, 36);
            }else{
                cell.dataLabel.frame = CGRectMake(90, 0, SCREEN_WIDTH-90, 40);
                cell.spaceLabel.frame = CGRectMake(80, 0, 10, 40);
            }
            if (indexPath.section == 0&&indexPath.row == 0) {
                cell.titleLabel.text = @"先前状态：";
                self.batteryTextField = [[UITextField alloc]initWithFrame:CGRectMake(90, 0, cell.contentView.frame.size.width-90, 36)];
                self.batteryTextField.delegate = self;
                self.batteryTextField.font = [UIFont systemFontOfSize:13.0];
                
                if (self.batteryData == nil) {
                    self.batteryTextField.text = [NSString stringWithFormat:@""];
                }else{
                    self.batteryTextField.text = [NSString stringWithFormat:@"%@",self.batteryData];
                }
                
                [cell.contentView addSubview:self.batteryTextField];
                
                UILabel * batteryStr = [[UILabel alloc]init];
                batteryStr.text = @"(电压值)";
                batteryStr.font = [UIFont systemFontOfSize:12.0];
                [cell.contentView addSubview:batteryStr];
                
                [batteryStr mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.equalTo(@-10);
                    make.top.equalTo(@10);
                    make.width.mas_equalTo(50);
                    make.height.mas_equalTo(30);
                }];

                self.baseLine = [[UILabel alloc]init];
                self.baseLine.backgroundColor = [UIColor blackColor];
                [cell.contentView addSubview:self.baseLine];
                [self.baseLine mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(cell.spaceLabel.mas_left);
                    make.bottom.equalTo(cell.contentView.mas_bottom).offset(-3);
                    make.right.equalTo(batteryStr.mas_left).offset(-10);
                    make.height.mas_equalTo(1);
                }];
                cell.spaceLabel.backgroundColor = [UIColor whiteColor];

            }
            
            if (indexPath.section == 1&&indexPath.row == 0) {
                cell.titleLabel.text = @"先前状态：";
                self.signalTextField = [[UITextField alloc]initWithFrame:CGRectMake(90, 0, cell.contentView.frame.size.width-90, 40)];
                self.signalTextField.delegate = self;
                self.signalTextField.font = [UIFont systemFontOfSize:13.0];
                if (self.signalData == nil) {
                    self.signalTextField.text = [NSString stringWithFormat:@""];
                }else{
                    self.signalTextField.text = [NSString stringWithFormat:@"%@",self.signalData];
                }
                [cell.contentView addSubview:self.signalTextField];
                
                UILabel * batteryStr = [[UILabel alloc]init];
                batteryStr.text = @"(信号值)";
                batteryStr.font = [UIFont systemFontOfSize:12.0];
                [cell.contentView addSubview:batteryStr];
                [batteryStr mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.equalTo(@-10);
                    make.top.equalTo(@10);
                    make.width.mas_equalTo(50);
                    make.height.mas_equalTo(30);
                }];
                
                self.baseLine = [[UILabel alloc]init];
                self.baseLine.backgroundColor = [UIColor blackColor];
                [cell.contentView addSubview:self.baseLine];
                [self.baseLine mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(cell.spaceLabel.mas_left);
                    make.bottom.equalTo(cell.contentView.mas_bottom).offset(-3);
                    make.right.equalTo(batteryStr.mas_left).offset(-10);
                    make.height.mas_equalTo(1);
                }];
                cell.spaceLabel.backgroundColor = [UIColor whiteColor];

            }
            if (indexPath.row == 1){
                cell.titleLabel.text = @"运维措施：";
                cell.iconImageView.image = [UIImage imageNamed:@"jiantou1"];
                cell.dataLabel.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
                cell.spaceLabel.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
                
            }
            if (indexPath.row == 2)
            {
                cell.titleLabel.text = @"运维结果：";
                cell.iconImageView.image = [UIImage imageNamed:@"jiantou1"];
                cell.dataLabel.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
                cell.spaceLabel.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
            }
            
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            return cell;
            
        }else{
            
            static NSString * iden = @"iden";
            UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
            if (cell == nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
            }
            
            if (indexPath.section == 2) {

                UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc]init];
                flow.scrollDirection = UICollectionViewScrollDirectionVertical;
                flow.itemSize = CGSizeMake(70, 90);
                self.photoCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 275) collectionViewLayout:flow];
                self.photoCollectionView.backgroundColor = [UIColor whiteColor];
//                self.titleArray = @[@"全景",@"东",@"西",@"南",@"北",@"太阳能板",@"端子连接线",@"雨量筒翻斗",@"雨量筒上侧"];
                self.photoCollectionView.delegate = self;
                self.photoCollectionView.dataSource = self;
                self.photoCollectionView.bounces = YES;
                [self.photoCollectionView registerClass:[PhotoCell class] forCellWithReuseIdentifier:@"photo"];
                [cell.contentView addSubview:self.photoCollectionView];
                [self.photoCollectionView reloadData];
                
            }
            
            if (indexPath.section == 3) {
                self.remarkTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
                self.remarkTextField.text = @"";
                self.remarkTextField.placeholder = @"  添加备注";
                self.remarkTextField.font = [UIFont systemFontOfSize:13.0];
                self.remarkTextField.delegate = self;
                if (self.remarkData == nil) {
                    self.remarkTextField.text = [NSString stringWithFormat:@""];
                }else{
                    self.remarkTextField.text = [NSString stringWithFormat:@"%@",self.remarkData];
                }
                [cell.contentView addSubview:self.remarkTextField];
                self.baseLine = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, 1)];
                self.baseLine.backgroundColor = [UIColor blackColor];
                [cell.contentView addSubview:self.baseLine];
                
            }
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            cell.contentView.backgroundColor = [UIColor whiteColor];
            return cell;
        }
        
    }


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (indexPath.section == 0||indexPath.section == 1) {
            return 40;
        }else if (indexPath.section == 2&&indexPath.row == 0) {
            return 275;
        }
        else{
            return 35;
        }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [self deselectRowAtIndexPath:indexPath animated:NO];
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*2)];
    self.bgView.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    [self.bgView addGestureRecognizer:tap];
    [self addSubview:self.bgView];
    
    NSDictionary * dict = [self jsonDict];
    NSArray * textArray = dict[@"list"];
    if (indexPath.row!=0) {
        
        self.listTableView = [[ListTableView alloc]initWithFrame:CGRectMake(80, [self rectForRowAtIndexPath:indexPath].origin.y+40, SCREEN_WIDTH-85, [textArray[indexPath.section][indexPath.row-1] count]*40) style:UITableViewStylePlain];
            self.listTableView.sectionTag = indexPath.section;
            self.listTableView.rowTag = indexPath.row;
            self.listTableView.tableViewTag = 100;
            self.listTableView.vcName = @"Maintain";
            self.listTableView.dataArray = textArray[indexPath.section][indexPath.row-1];
            [self.listTableView reloadData];
            [self addSubview:self.listTableView];
        
    } 
}

#pragma TextField - delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    if (textField == self.batteryTextField) {
        textField.text = @"电压值：";
    }
    if (textField == self.signalTextField) {
        textField.text = @"信号值：";
    }
    if (textField == self.remarkTextField) {
        textField.text = @"";
    }
    if (textField == self.headView.personTextField) {
        textField.text = @"";
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if (textField == self.batteryTextField)
    {
        self.batteryData = textField.text;
    }
    if (textField == self.signalTextField) {
        self.signalData = textField.text;
    }
    if (textField == self.remarkTextField) {
        self.remarkData = textField.text;
    }
    if (textField == self.headView.personTextField) {
        self.personData = textField.text;
    }
}

#pragma ScrollView - delegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    [self reloadData];
}

#pragma UICollectionView - delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _titleArray.count;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    return 5;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake(0, 5, 0, 5);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(70, 90);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    PhotoCell * cell = (PhotoCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"photo" forIndexPath:indexPath];
    cell.titileLabel.text = self.titleArray[indexPath.row];
    cell.photoImageView.image = self.photoArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [self.pickDelegate jumptovc:indexPath.row];
}

#pragma mark 自定义Delegate

- (void)jumptovc:(NSInteger)tag {
    
}

@end
